package com.lsy.weizhi2.mysql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.LinearLayoutManager;

import com.lsy.weizhi2.JsonObject.ActivityListObject;
import com.lsy.weizhi2.JsonObject.SelectLevel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 嗨哟 on 2017/4/19.
 */
public class MySQLOperate {
    private SQLiteDatabase sqLiteWrite, sqLiteRead;
    private static MySQLOperate operate;

    private MySQLOperate(Context context) {
        sqLiteRead = MySQLiteHelper.getInstance(context).getReadableDatabase();
        sqLiteWrite = MySQLiteHelper.getInstance(context).getWritableDatabase();
    }

    public static MySQLOperate getInstance(Context context) {
        if (operate == null) {
            operate = new MySQLOperate(context);
        }
        return operate;
    }

    public void Insert(String table, ContentValues values, int id) {
        Cursor cursor = sqLiteRead.query(table, null, "id=?", new String[]{id + ""}, null, null, null, null);
        if (cursor != null && cursor.getCount() != 0) {
            sqLiteWrite.update(table, values, "id=?", new String[]{id + ""});
        } else {
            sqLiteWrite.insert(table, null, values);
        }
    }

    public ActivityListObject.ListBean Select_Activity(int id) {
        Cursor cursor = sqLiteRead.query("activity", null, "id=?", new String[]{id + ""}, null, null, null, null);
        ActivityListObject.ListBean bean = new ActivityListObject.ListBean();
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String email = cursor.getString(cursor.getColumnIndex("email"));
            String create_time = cursor.getString(cursor.getColumnIndex("create_time"));
            String photo = cursor.getString(cursor.getColumnIndex("photo"));
            int avatar = cursor.getInt(cursor.getColumnIndex("avatar"));
            String tel = cursor.getString(cursor.getColumnIndex("tel"));
            int _id = cursor.getInt(cursor.getColumnIndex("id"));
            int enrollment = cursor.getInt(cursor.getColumnIndex("enrollment"));
            bean.setAvatar(avatar);
            bean.setTel(tel);
            bean.setId(_id);
            bean.setTitle(title);
            bean.setContent(content);
            bean.setEmail(email);
            bean.setRealName(name);
            bean.setCreateTime(create_time);
            bean.setPhoto(photo);
            bean.setEnrollment(enrollment);
        }
        return bean;
    }

    public List<SelectLevel> Select_HomeGrid() {
        Cursor cursor = sqLiteRead.query("homegrid", null, null, null, null, null, null, null);
        List<SelectLevel>list=new ArrayList<>();
        while (cursor.moveToNext()) {
            SelectLevel level = new SelectLevel();
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int first = cursor.getInt(cursor.getColumnIndex("first"));
            int second = cursor.getInt(cursor.getColumnIndex("second"));
            int place = cursor.getInt(cursor.getColumnIndex("place"));
            level.setName(name);
            level.setId(second);
            level.setFid(first);
            level.setImage(place);
            list.add(level);
        }
        return list;
    }

    public void Delete(String table,int id) {
        sqLiteWrite.delete(table, "id=?", new String[]{id+""});
    }

}
